package edu.gatech.volt2.drdoommgt.presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JToggleButton;

import edu.gatech.volt2.drdoommgt.gui.*;
import edu.gatech.volt2.drdoommgt.domain.*;
import edu.gatech.volt2.drdoommgt.system.DataBaseAccessor;

public class UserInfoPresenter implements ActionListener{

	private static DataBaseAccessor dba;
	private IUserInfoView myView;
	private int type;
	
	public UserInfoPresenter(int type,IUserInfoView view){
		this.type= type;
		this.myView = view;
		this.myView.addListener(this);
		this.myView.showSelf();
		dba = new DataBaseAccessor();
		dba.connectToDatabase();
	}

	public UserInfoPresenter(int type2, DataBaseAccessor dba2) {
		dba = dba2;
		this.type= type2;
		this.myView = new UserInformationWindow();
		this.myView.addListener(this);
		this.myView.showSelf();
	}

	public void actionPerformed(ActionEvent action) {
		if(action.getSource()== myView.getCancelButton()){
			myView.hideSelf();
		}
		else if(action.getSource()== myView.getSaveButton()){

			if(type == 0)
			{
				createPatientProcess();
			}
			else if(type==1)
			{
				createDoctorProcess();
			}
			else if(type==2)
			{
				createNurseProcess();
			}
			else if(type==3)
			{
				createSysAdminProcess();
			}
			else{
				//do nothing. the sys admin canceled the task
			}
			
			myView.hideSelf();
		}
		else{/*do nothing*/}
		
	}
	
	private void createPatientProcess(){
		Patient patient= null;
		
		JTextField firstNameField= myView.getFirstNameField();
		JTextField middleNameField= myView.getMiddleNameField();
		JTextField lastNameField= myView.getLastNameField();
		JTextField zipCodeField= myView.getZipCodeField();
		JTextField emailField= myView.getEmailField();
		JTextField phone= myView.getPhone();
		JTextField addressField= myView.getAddressField();
		JTextField cityField= myView.getCityField();
		JRadioButton rdbtnMale= myView.getRdbtnMale();
		JComboBox monthBox= myView.getMonthBox();
		JComboBox dayBox= myView.getDayBox();
		JComboBox yearBox= myView.getYearBox();
		JComboBox stateComboBox= myView.getStateComboBox();
		
		Name name= new Name(firstNameField.getText(),middleNameField.getText(),lastNameField.getText());
		Address addr= new Address(addressField.getText(), cityField.getText(), (String)stateComboBox.getSelectedItem(), zipCodeField.getText());
		Calendar birthday= Calendar.getInstance();
		birthday.set(Integer.parseInt((String)yearBox.getSelectedItem()), Integer.parseInt((String)monthBox.getSelectedItem()) - 1, Integer.parseInt((String)dayBox.getSelectedItem()));
		String gender= (rdbtnMale.isSelected() ? "male" : "female");
		
		patient= new Patient(name);
		patient.setEmail(emailField.getText());
		PatientInfo pi= new PatientInfo(gender, birthday ,null, phone.getText(), addr, null);
		patient.setInfo(pi);
		
		String password= JOptionPane.showInputDialog("Enter a desired password");
		patient.setPassword(password);
		
		dba.save(patient);
	}
	
	private void createDoctorProcess(){
		Doctor doctor= null;
		
		JTextField firstNameField= myView.getFirstNameField();
		JTextField middleNameField= myView.getMiddleNameField();
		JTextField lastNameField= myView.getLastNameField();
		JTextField emailField= myView.getEmailField();
		
		Name name= new Name(firstNameField.getText(),middleNameField.getText(),lastNameField.getText());
		
		doctor= new Doctor(name);
		doctor.setEmail(emailField.getText());
		
		String password= JOptionPane.showInputDialog("Enter a desired password");
		doctor.setPassword(password);

		
		dba.save(doctor);
	}
	
	private void createNurseProcess(){
		Nurse nurse= null;
		
		JTextField firstNameField= myView.getFirstNameField();
		JTextField middleNameField= myView.getMiddleNameField();
		JTextField lastNameField= myView.getLastNameField();
		JTextField emailField= myView.getEmailField();
		
		Name name= new Name(firstNameField.getText(),middleNameField.getText(),lastNameField.getText());
		
		nurse= new Nurse(name);
		nurse.setEmail(emailField.getText());
		
		String password= JOptionPane.showInputDialog("Enter a desired password");
		nurse.setPassword(password);

		
		dba.save(nurse);
	}
	
	private void createSysAdminProcess(){
		SysAdmin sysAdmin= null;
		
		JTextField firstNameField= myView.getFirstNameField();
		JTextField middleNameField= myView.getMiddleNameField();
		JTextField lastNameField= myView.getLastNameField();
		JTextField emailField= myView.getEmailField();
		
		Name name= new Name(firstNameField.getText(),middleNameField.getText(),lastNameField.getText());
		
		sysAdmin= new SysAdmin(name);
		sysAdmin.setEmail(emailField.getText());
		
		String password = JOptionPane.showInputDialog("Enter a desired password");
		sysAdmin.setPassword(password);

		
		dba.save(sysAdmin);
	}
	
	
}
